package cn.otrue.patienthealthmanager.activity;

import java.util.Calendar;
import java.util.HashMap;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.bean.PatientModel;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.utils.DensityUtil;
import cn.otrue.patienthealthmanager.utils.MyConst;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

/**
 * 个人中心->个人信息
 * 
 * @author G
 * 
 */
public class CenterInfoActivity extends BaseActivity {
	private TextView tv_title_title;

	private TextView tv_name;
	private TextView tv_sex;
	private TextView tv_bir;
	private TextView tv_nicheng;
	private TextView tv_shouji;
	private TextView tv_bingli;
	private TextView tv_zhuzhi;
	private PatientModel tuuu;
	private String dateStr;
	private String birthday;
	private String img;
	private ImageView pe_head_image;

	public void ImageSet(String imgurl, ImageView img_headPhoto) {

		DisplayImageOptions options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.iv_user_photo)
				.showImageOnFail(R.drawable.iv_user_photo)
				.cacheInMemory(true)
				.cacheOnDisk(true)
				.bitmapConfig(Bitmap.Config.RGB_565)
				.displayer(
						new RoundedBitmapDisplayer(DensityUtil.dip2px(
								CenterInfoActivity.this, 40))).build();
		ImageLoader.getInstance().displayImage(imgurl, img_headPhoto, options);
	}
public static CenterInfoActivity centerinfos;
	// //////////////////////////////////////////////////////////////////////////首先加载数据，若数据为null则进入填写详细信息页
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_center_info);
		centerinfos=this;
		initView();

	}

	private void initView() {
		initTitleBar();
		id_nianling = (TextView) findViewById(R.id.id_nianling);
		pe_head_image = (ImageView) findViewById(R.id.pe_head_image);
		tv_name = (TextView) findViewById(R.id.tv_name);
		tv_sex = (TextView) findViewById(R.id.tv_sex);
		tv_bir = (TextView) findViewById(R.id.tv_bir);
		tv_nicheng = (TextView) findViewById(R.id.tv_nicheng);
		tv_shouji = (TextView) findViewById(R.id.tv_shouji);
		tv_bingli = (TextView) findViewById(R.id.tv_bingli);
		tv_zhuzhi = (TextView) findViewById(R.id.tv_zhuzhi);
		if (MyConst.PATIENT != null) {
			tuuu=MyConst.PATIENT;
			setData();
		} else {
			inti();
		}

	}

	// private DisplayImageOptions options = new DisplayImageOptions.Builder()
	// .showImageOnFail(R.drawable.iv_user_photo)
	// .showImageOnLoading(R.drawable.iv_user_photo)
	// .showImageForEmptyUri(R.drawable.iv_user_photo).cacheInMemory(true)
	// .cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();

	public void initTitleBar() {
		tv_title_title = (TextView) findViewById(R.id.tv_title_title);
		tv_title_title.setText(getString(R.string.centerinfo_title_content));

		TextView tv_title_back = (TextView) findViewById(R.id.tv_title_back);
		TextView tv_title_oth = (TextView) findViewById(R.id.tv_title_ot);

		tv_title_oth.setVisibility(View.VISIBLE);
		// tv_title_oth.setBackgroundResource(resid);
		tv_title_oth.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(CenterInfoActivity.this,
						CenterInfoChangeActivity.class);
				intent.putExtra("isComplete", 1);
				if (tuuu.getRealname() != null) {
					intent.putExtra("Realname", tuuu.getRealname());
				}
				if (tuuu.getBirthday() != null
						&& !"".equals(tuuu.getBirthday())) {
					intent.putExtra("Birthday", tuuu.getBirthday().toString());
				}
				if (tuuu.getNickName() != null) {
					intent.putExtra("NickName", tuuu.getNickName().toString());
				}
				if (tuuu.getMobile() != null) {
					intent.putExtra("Mobile", tuuu.getMobile().toString());
				}
				if (tuuu.getDiseasehistory() != null) {
					intent.putExtra("Diseasehistory", tuuu.getDiseasehistory()
							.toString());
				}
				if (tuuu.getAddress() != null) {
					intent.putExtra("Address", tuuu.getAddress().toString());
				}
				if (tuuu.getSex() != null) {
					intent.putExtra("Sex", tuuu.getSex().toString());
				}
				if (tuuu.getHeadimg() != null) {
					intent.putExtra("Headimg", tuuu.getHeadimg().toString());
				}

				startActivity(intent);
				
			}
		});

		tv_title_back.setVisibility(View.VISIBLE);
		tv_title_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

	}

	// private void ddddd(){
	//
	//
	// final ImageLoader loader = ImageLoader.getInstance();
	// loader.displayImage(img, pe_head_image, options);
	// }
	private Handler Handler = new ServerHandler() {

		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			String tuu = data.getString(HttpCons.DATA);
			if (state.equals("001")) {

				Gson gson = HttpServer.gson;

				tuuu = gson.fromJson(tuu, PatientModel.class);
				MyConst.PATIENT=tuuu;
				setData();
				Toast.makeText(CenterInfoActivity.this, "操作成功", 0).show();
			} else {
				Toast.makeText(CenterInfoActivity.this, "msg", 0).show();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {

			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(CenterInfoActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();

			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};

	public void setData() {
		if (tuuu.getRealname() != null) {
			tv_name.setText(tuuu.getRealname());
			if (tuuu.getSex().toString().equals("1")) {
				tv_sex.setText("男");
			} else {
				tv_sex.setText("女");

			}
		} else {

		}
		if (tuuu.getHeadimg() != null) {
			img = tuuu.getHeadimg().toString();
			ImageSet(img, pe_head_image);
		} else {
		}
		if (tuuu.getBirthday() != null) {
			tv_bir.setText(tuuu.getBirthday().toString());
		}
		dateStr = birthday = tuuu.getBirthday();
		if (birthday != null) {
			String[] dateArr = dateStr.split("-");
			int nianling = Integer.parseInt(dateArr[0]);

			Calendar calendar = Calendar.getInstance();
			int year = calendar.get(Calendar.YEAR);
			id_nianling.setText((year - nianling) + "");
		}
		if (tuuu.getNickName() != null) {
			tv_nicheng.setText(tuuu.getNickName().toString());

		} else {
		}
		if (tuuu.getMobile() != null) {
			tv_shouji.setText(tuuu.getMobile().toString());
		} else {
		}
		if (tuuu.getDiseasehistory() != null) {
			tv_bingli.setText(tuuu.getDiseasehistory().toString());

		} else {
		}
		if (tuuu.getAddress() != null) {
			tv_zhuzhi.setText(tuuu.getAddress().toString());
		}
	}

	private TextView id_nianling;

	private void inti() {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("aliasAccount", MyConst.getACCOUNT());
		TransferParameter parameter = new TransferParameter();
		parameter.setUsername("");
		parameter.setPassword("");
		parameter.setCurrentPage("");
		parameter.setPagesize("");
		parameter.setData(map);
		new HttpServer()
				.post(HttpCons.queryPatientBaseinfo, parameter, Handler);

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.ll_center_info_case:
			Intent medicalrecorddIntent = new Intent(CenterInfoActivity.this,
					MedicalRecord.class);
			startActivity(medicalrecorddIntent);
			break;
		}
	}
}
